package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.CouponDao;
import com.delivery.www.po.Coupon;
import com.delivery.www.po.HaveCoupon;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 12970
 */
public class CouponDaoImpl implements CouponDao {
    @Override
    public List<Coupon> getCoupon(int storeId) {
        Connection connection = JdbcUtils.getConnection();
        List<Coupon> coupons = null;
        Object[] params = {storeId};
        String sql = "select c.coupon_id,store_id,value,min,validity from coupon c where store_id = ?";
        if (connection != null) {
            coupons = BaseDao.getList(connection, Coupon.class, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return coupons;
    }

    @Override
    public int addCoupon(int storeId, Float value, Float min, int validity) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {storeId, value, min, validity};
        String sql = "insert into coupon (store_id,value,min,validity) values (?,?,?,?)";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return excute;
    }

    @Override
    public int deleteCoupon(int couponId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {couponId};
        String sql = "delete from coupon where coupon_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            if(excute>0){
                String s = "delete from have_coupon where coupon_id = ?";
                BaseDao.executeUpdate(connection,s,couponId);
            }
        }
        JdbcUtils.closeResource(connection, null, null);
        return excute;
    }

    @Override
    public Coupon getOneCoupon(int couponId) {
        Connection connection = JdbcUtils.getConnection();
        Coupon coupon = null;
        Object[] params = {couponId};
        String sql = "select coupon_id,store_id,value,min,validity from coupon where coupon_id = ?";
        if (connection != null) {
            coupon = BaseDao.getForOne(connection, Coupon.class, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return coupon;
    }

    @Override
    public int modifyCouponValue(int couponId, Float value) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {value, couponId};
        String sql = "update coupon set value = ? where coupon_id = ?";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return i;
    }

    @Override
    public int modifyCouponMin(int couponId, Float value) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {value, couponId};
        String sql = "update coupon set min = ? where coupon_id = ?";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return i;
    }

    @Override
    public int modifyCouponValidity(int couponId, int value) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {value, couponId};
        String sql = "update coupon set validity = ? where coupon_id = ?";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return i;
    }

    @Override
    public List<Coupon> getUserCoupon(List<HaveCoupon> list) {
        Connection connection = JdbcUtils.getConnection();
        List<Integer> integers = new ArrayList<>();
        List<Coupon> coupons = new ArrayList<>();
        for (HaveCoupon haveCoupon : list) {
            Integer couponId = haveCoupon.getCouponId();
            integers.add(couponId);
        }
        Object[] params = integers.toArray(new Integer[0]);
        String sql = "select c.coupon_id,c.store_id,value,min,validity,s.store_name from coupon c left join store s on s.store_id = c.store_id where coupon_id = ?";
        for (Object param : params) {
            Coupon coupon = BaseDao.getForOne(connection, Coupon.class, sql, param);
            coupons.add(coupon);
        }
        JdbcUtils.closeResource(connection, null, null);
        return coupons;
    }

    @Override
    public List<Coupon> getAllCoupon() {
        Connection connection = JdbcUtils.getConnection();
        List<Coupon> coupons = null;
        String sql = "select c.coupon_id,store_id,value,min,validity from coupon c ";
        if (connection != null) {
            coupons = BaseDao.getList(connection, Coupon.class, sql);
        }
        JdbcUtils.closeResource(connection, null, null);
        return coupons;
    }
}
